package com.example.prometheus.controller;

import com.example.prometheus.model.Order;
import com.example.prometheus.service.BeerService;
import com.sjyt.base.metrics.config.AbstractMetrics;
import com.sjyt.base.metrics.config.Micrometer;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Timer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RequestMapping("/order")
@RestController
public class OrderController extends AbstractMetrics {

    @Micrometer(name = "order.change.event", tags = {"event", "create", "exec_res", "success"})
    private Counter orderCreateCounter;
    @Micrometer(name = "order.change.event", tags = {"event", "create", "exec_res", "failed"})
    private Counter orderFailedCreateCounter;

    @Micrometer(name = "order.change.event", tags = {"event", "change", "exec_res", "time"})
    private Timer orderChangeTimer;

    @Autowired
    private BeerService beerService;


    /**
     * # HELP http_server_requests_seconds 创建订单时间消耗
     * # TYPE http_server_requests_seconds summary
     * http_server_requests_seconds_count{application="yt.prometheus",exception="None",method="GET",outcome="SUCCESS",role="master",status="200",test_order="create",uri="/order/create/{type}",} 3.0
     * http_server_requests_seconds_sum{application="yt.prometheus",exception="None",method="GET",outcome="SUCCESS",role="master",status="200",test_order="create",uri="/order/create/{type}",} 6.050759301
     * # HELP http_server_requests_seconds_max 创建订单时间消耗
     * # TYPE http_server_requests_seconds_max gauge
     * http_server_requests_seconds_max{application="yt.prometheus",exception="None",method="GET",outcome="SUCCESS",role="master",status="200",test_order="create",uri="/order/create/{type}",} 0.0
     */
    @GetMapping("/notify")
    @Timed(description = "创建订单时间消耗")
    public String pay_notify() {
        int type = new Random(System.currentTimeMillis()).nextInt(100);
        try {
            Thread.sleep(type * 10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if(type == 50){
            throw new RuntimeException("error");
        }
        return "ok";
    }


    @GetMapping("/error")
    public String error() {
        throw new RuntimeException("error");
    }

    @GetMapping("/placeorder")
    public String placeorder() {
        int type = new Random(System.currentTimeMillis()).nextInt(100);
        if (type > 50) {
            increment("order.create.success");
        } else {
            increment("order.create.failed");
        }
        orderChangeTimer.record(() -> {
            try {
                Thread.sleep(type * 10);
                beerService.orderBeer(new Order(type, type > 50 ? "light" : "ale"));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        return "Ok";
    }

    @Override
    public void initCounter() {
        register("order.create.success", orderCreateCounter);
        register("order.create.failed", orderFailedCreateCounter);
    }
}
